package de.fhd.pms.ctrl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.fhd.pms.dao.AutoDao;
import de.fhd.pms.dao.DeleteException;
import de.fhd.pms.dao.KundeDao;
import de.fhd.pms.dao.StationDao;
import de.fhd.pms.model.Auto;
import de.fhd.pms.model.Station;

/**
 * The Controller class for {@link Auto} objects. Its tasks:
 * <ul>
 * <li>read and verify the parameters</li>
 * <li>access to the objects of the system</li>
 * <li>selection of the view and provision of content for the view</li>
 * </ul>
 * 
 * @author Josef Fischer
 * @author Alexander Neetzel
 * @author Philipp Six
 * 
 */
@Controller
public class AutoController {
	private static Logger log = Logger.getLogger(AutoController.class);
	private AutoDao autoDao = null;
	private StationDao stationDao = null;
	private KundeDao kundeDao = null;

	public KundeDao getKundeDao() {
		return kundeDao;
	}

	public void setKundeDao(KundeDao kundeDao) {
		this.kundeDao = kundeDao;
	}

	public void setAutoDao(AutoDao autoDao) {
		this.autoDao = autoDao;
	}

	public void setStationDao(StationDao stationDao) {
		this.stationDao = stationDao;
	}

	/**
	 * Its task is to create a new {@link Auto} object.
	 * 
	 * <p>
	 * Expected HTTP GET and request '/SetAuto.html'.
	 * </p>
	 * 
	 * @param id
	 * 
	 * @return View name 'admin-set-auto' selecting view
	 *         '/view/admin-set-auto.jsp'.
	 */
	@RequestMapping(value = "/SetAuto.html", method = RequestMethod.GET)
	public ModelAndView set(@RequestParam(required = false) Integer id) {
		log.debug("SetAuto.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Auto());
		} else {
			mv.addObject(autoDao.findById(id));
		}
		mv.addObject("station", stationDao.findAll());
		mv.setViewName("admin-set-auto");
		return mv;
	}

	/**
	 * <p>
	 * Saves a {@link Auto} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/SaveAuto.html'.
	 * </p>
	 * 
	 * @param auto
	 * @param stationId
	 * @param model
	 * @return Redirect to '/AllAvailableAuto.html'.
	 */
	@RequestMapping(value = "/SaveAuto.html", method = RequestMethod.POST)
	public String save(Auto auto, int stationId, Model model) {
		log.debug("SaveAuto.html autoId=" + auto.getId() + " stationId="
				+ stationId);
		if (auto.getCreated() == null) {
			auto.setCreated(new Date());
		}
		Station station = stationDao.findById(stationId);
		auto.setStation(station);
		autoDao.save(auto);
		model.addAttribute("statusMessageKey", "kunde.form.msg.success");
		return "redirect:AllAvailableAuto.html";
	}

	/**
	 * <p>
	 * Deletes a {@link Auto} object.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/DeleteAuto.html'.
	 * </p>
	 * 
	 * @param auto
	 * @throws DeleteException
	 *             if the specific {@link Auto} was already in use.
	 * @return Redirect to '/AllAvailableAuto.html'.
	 */
	@RequestMapping(value = "/DeleteAuto.html", method = RequestMethod.GET)
	public ModelAndView delete(Auto auto) {

		try {
			autoDao.delete(auto);
			return new ModelAndView("redirect:AllAvailableAuto.html");
		} catch (DeleteException e) {
			ModelAndView mv = new ModelAndView("admin-error");
			mv.addObject("message", e.getMessage());
			return mv;

		}
	}

	/**
	 * <p>
	 * Searches for all available {@link Auto} objects and returns them in a
	 * <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/AllAvailableAuto.html'.
	 * </p>
	 * 
	 * @return View name 'admin-list-auto' selecting view
	 *         '/view/admin-list-auto.jsp'.
	 */
	@RequestMapping(value = "/AllAvailableAuto.html", method = RequestMethod.GET)
	public ModelAndView findAllAvailable() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Auto bearbeiten");
		mv.addObject("message", "Alle verfügbaren Autos");
		mv.addObject("autos", autoDao.findAvailable());
		mv.addObject("edit", true);
		mv.setViewName("admin-list-auto");
		return mv;
	}

	/**
	 * <p>
	 * Searches for all available {@link Auto} objects and returns them in a
	 * <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/KundeAuto.html'.
	 * </p>
	 * 
	 * @return View name 'kunde-list-auto' selecting view
	 *         '/view/kunde-list-auto.jsp'.
	 */
	@RequestMapping(value = "/KundeAuto.html", method = RequestMethod.GET)
	public ModelAndView availableAuto() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Fahrt beginnen");
		mv.addObject(
				"message",
				"Hier sehen Sie alle zurzeit verfügbaren Fahrzeuge, die auf einer Station geparkt wurden. Bitte wählen Sie ein Auto für eine Fahrt aus.");
		mv.addObject("autos", autoDao.findAvailable());
		mv.addObject("edit", false);
		mv.setViewName("kunde-list-auto");
		return mv;
	}
}
